<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="入库编号" prop="stockInCode">
        <el-input
          v-model="queryParams.stockInCode"
          placeholder="请输入入库编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单编号" prop="orderCode">
        <el-input
          v-model="queryParams.orderCode"
          placeholder="请输入订单编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="总成本" prop="cost">
        <el-input
          v-model="queryParams.cost"
          placeholder="请输入总成本"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建人" prop="createBy">
        <el-input
          v-model="queryParams.createBy"
          placeholder="请输入创建人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="创建时间" prop="createDate">
        <el-date-picker clearable
                        v-model="queryParams.createDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择创建时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="修改人" prop="modifyBy">
        <el-input
          v-model="queryParams.modifyBy"
          placeholder="请输入修改人"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="修改时间" prop="modifyDate">
        <el-date-picker clearable
                        v-model="queryParams.modifyDate"
                        type="date"
                        value-format="yyyy-MM-dd"
                        placeholder="请选择修改时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['store:stockIn:add']"
        >新增
        </el-button>
      </el-col>
      <AddStockIn @updList="getList" :updData="this.updData" ref="asi"></AddStockIn>

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['store:stockIn:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['store:stockIn:export']"
        >导出
        </el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <StockInView :viewData="this.viewData" ref="siv"></StockInView>
    <el-table v-loading="loading" :data="stockInList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="序号" align="center">
        <template slot-scope="scope">
          {{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="入库编号" width="200" show-overflow-tooltip align="center" prop="stockInCode">
        <template slot-scope="scope">
          <el-button type="text" @click="showView(scope.row)">{{
              scope.row.stockInCode
            }}
          </el-button>
        </template>
      </el-table-column>
      <el-table-column label="订单编号" width="200" show-overflow-tooltip align="center" prop="orderCode"/>
      <el-table-column label="入库类型" width="150" align="center" prop="typeName"/>
      <el-table-column label="仓库名称" align="center" prop="houseName"/>
      <el-table-column label="合格数量" align="center" prop="passNum"/>
      <el-table-column label="不合格数量" align="center" prop="noPassNum"/>
      <el-table-column label="总数量" align="center" prop="totalNum"/>
      <el-table-column label="合格率" align="center" prop="passRate">
        <template slot-scope="scope">
          {{ scope.row.passRate * 100 + "%" }}
        </template>
      </el-table-column>
      <el-table-column label="合格成本" align="center" prop="passCost"/>
      <el-table-column label="总成本" align="center" prop="cost"/>
      <el-table-column label="已核销金额" align="center" prop="paidAmount"/>
      <el-table-column label="创建人" align="center" prop="createBy"/>
      <el-table-column label="创建时间" align="center" prop="createDate" width="180">
      </el-table-column>
      <el-table-column label="修改人" align="center" prop="modifyBy"/>
      <el-table-column label="修改时间" align="center" prop="modifyDate" width="180">
      </el-table-column>
      <el-table-column label="操作" width="150" fixed="right" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            :disabled="scope.row.status==1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['store:stockIn:edit']"
          >修改
          </el-button>
          <el-button
            :disabled="scope.row.status==1"
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['store:stockIn:remove']"
          >删除
          </el-button>
          <el-button
            :disabled="scope.row.status==1"
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleConfirm(scope.row)"
            v-hasPermi="['store:stockIn:add']"
          >确认入库
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

  </div>
</template>

<script>
import {listStockIn, getStockIn, delStockIn, addStockIn, updateStockIn} from "@/api/store/stockIn";
import StockInView from "@/views/store/stockIn/StockInView.vue";
import AddStockIn from "@/views/store/stockIn/AddStockIn.vue";
import AddSaleBack from "@/views/xiaoshou/saleBack/AddSaleBack.vue";
import request from '@/utils/request'

export default {
  name: "StockIn",
  components: {AddSaleBack, StockInView, AddStockIn},
  data() {
    return {
      updData: {},
      viewData: {},
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 入库记录表格数据
      stockInList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stockInCode: null,
        orderCode: null,
        inventoryType: null,
        cost: null,
        createBy: null,
        createDate: null,
        modifyBy: null,
        modifyDate: null,
        isDel: null
      },
      // 表单参数
      form: {},
    };
  },
  created() {
    this.getList();
  },
  methods: {
    handleConfirm(row) {
      console.log(row)
      request({
        url: "store/stockIn/confirmIn",
        data: row,
        method: "post"
      }).then(res => {
        console.log(res)
        if (res.code == 200) {
          this.$modal.msgSuccess(res.msg);
          this.getList();
        }
      })
    },
    /*查看详情*/
    showView(row) {
      this.viewData = row;
      this.$refs.siv.showView();
    },
    /** 查询入库记录列表 */
    getList() {
      this.loading = true;
      listStockIn(this.queryParams).then(response => {
        this.stockInList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        stockInCode: null,
        orderCode: null,
        inventoryType: null,
        cost: null,
        createBy: null,
        createDate: null,
        modifyBy: null,
        modifyDate: null,
        isDel: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.$refs.asi.showAdd();
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.updData = row;
      this.$refs.asi.showUpd();
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除入库记录编号为"' + ids + '"的数据项？').then(function () {
        return delStockIn(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('store/stockIn/export', {
        ...this.queryParams
      }, `stockIn_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
